package com.neusoft.bizcore.activiti.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.activiti.dto.LockDataDTO;
import com.neusoft.bizcore.activiti.service.LockDataService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "工作流数据锁管理")
@RestController
@RequestMapping(value = "/api/w/act/locks")
public class ActLockDataController {
    @Autowired
    private LockDataService lockDataService;

    @ApiOperation("查询所有锁定的业务数据")
    @GetMapping
    public ResultListDTO<LockDataDTO> index(final Searchable searchable, final UserBean user) {
        final List<LockDataDTO> DTOs = this.lockDataService.index(searchable);
        final ResultListDTO<LockDataDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有锁定的业务数据")
    @GetMapping("/p")
    public PageResultDTO<LockDataDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.lockDataService.search(pageable, searchable));
    }

    @ApiOperation("创建锁定的业务数据")
    @PostMapping
    public ResultDTO<LockDataDTO> create(@RequestBody final LockDataDTO dto) {
        final LockDataDTO result = this.lockDataService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("锁定的业务数据详情")
    @GetMapping("/{id}")
    public ResultDTO<LockDataDTO> details(@PathVariable final Long id) {
        final LockDataDTO dto = this.lockDataService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("根据业务编码查询锁定的业务数据详情")
    @GetMapping("/code/{code}")
    public ResultDTO<LockDataDTO> findByCode(@PathVariable final String code) {
        final LockDataDTO dto = this.lockDataService.findByCode(code);
        return ResultDTO.success(dto);
    }

    @ApiOperation("根据workNum解除锁定的业务数据")
    @DeleteMapping("/unlock/{workNum}")
    public ResultDTO<Void> unlock(@PathVariable final String workNum) {
        this.lockDataService.deleteByWorkNum(workNum);
        return ResultDTO.success();
    }

    @ApiOperation("删除锁定的业务数据")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.lockDataService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改锁定的业务数据")
    @PutMapping("/{id}")
    public ResultDTO<LockDataDTO> update(@PathVariable final Long id,
            @RequestBody final LockDataDTO dto) {
        final LockDataDTO result = this.lockDataService.update(id, dto);
        return ResultDTO.success(result);
    }
}
